package com.wl.zlmanage.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.wl.zlmanage.bean.ApplyInfo;
import com.wl.zlmanage.bean.ApplySignInfo;
import com.wl.zlmanage.bean.ApprovalInfo;
import com.wl.zlmanage.bean.Profit;
import com.wl.zlmanage.bean.ProfitCheck;
import com.wl.zlmanage.bean.SignMatchInfo;
import com.wl.zlmanage.bean.User;
import com.wl.zlmanage.bean.ZlInfo;
import com.wl.zlmanage.bean.ZlSignInfo;
import com.wl.zlmanage.constant.ActivityConstant;
import com.wl.zlmanage.mapper.ApplyMapper;
import com.wl.zlmanage.mapper.SignCustomMapper;
import com.wl.zlmanage.mapper.SignMatchInfoMapper;
import com.wl.zlmanage.mapper.UserMapper;
import com.wl.zlmanage.utils.IOUtil;
import com.wl.zlmanage.utils.TimeUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * Created by wanglei on 2018/5/25.
 */
@Controller
public class MatchController {

    private Logger logger = LoggerFactory.getLogger(MatchController.class);

    @Autowired
    ApplyMapper applyDao;

    @Autowired
    SignCustomMapper signCustomDao;

    @Autowired
    UserMapper userDao;

    @Autowired
    SignMatchInfoMapper signMatchInfoDao;

    private List<ProfitCheck> pcList;

    @RequestMapping("/getSignMatchList.do")
    @ResponseBody
    public Map<String, Map<String, Double>> getSignMatchList() {
        pcList = new ArrayList<>();
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date endDate = ca.getTime();
        Calendar ca1 = Calendar.getInstance();
        ca1.add(Calendar.MONTH, -2);
        ca1.set(Calendar.DAY_OF_MONTH, 1);
        Date begainDate = ca1.getTime();

        List<String> monthList = getBeforeThreeMonth();
        List<SignMatchInfo> reList = signMatchInfoDao
            .getSignMatchList(TimeUtils.getDateString(begainDate, TimeUtils.DATE_DAY_FORMAT),
                TimeUtils.getDateString(endDate, TimeUtils.DATE_DAY_FORMAT));
        Double expendQyTotal = 0d;
        Double expendRcTotal = 0d;
        Double maxProfit = 0d;
        Double nowProfit = 0d;
        Double thisProfit = 0d;
        BigDecimal matchCount = null;
        //BigDecimal expendQyTotalDecimal = null;
        BigDecimal expendQyMonTotalDecimal = null;
        //BigDecimal expendQyTotalDecimalAvg = null;
        BigDecimal expendQyMonTotalDecimalAvg = null;
        Integer matchId;
        Integer qySignId;
        Integer personSignId;
        Double applyRcTotal = 0d;
        Double applyQyTotal = 0d;
        Double nowRencaiProfit = 0d;
        Double nowQyProfit = 0d;
        Double allExpend = 0d;
        Map<String, Map<String, Double>> profitAccountMap = new HashMap<>();
        Map<Integer, Double> rencaiMap = null;
        Map<String, Double> qiyeMap = null;

        Map<String,Double> checkedMap = new HashMap<>();
        for (SignMatchInfo signMatchInfo : reList) {
            ProfitCheck pc = new ProfitCheck();
            matchId = signMatchInfo.getMatchId();
            qySignId = signMatchInfo.getSignId();
            personSignId = signMatchInfo.getPersonSignId();

            logger.info("企业部ID:" + qySignId);
            logger.info("人才部ID:" + personSignId);
            pc.setQid(qySignId);
            pc.setRid(personSignId);
            pc.setqName(signMatchInfo.getqName());
            pc.setrName(signMatchInfo.getrName());
//            expendRcTotal = signMatchInfoDao.getExpendTotal(personSignId, 1,null);
//            if (null == expendRcTotal) {
//                expendRcTotal = 0d;
//            }
//            logger.info("人才部报销：" + expendRcTotal);
//            expendQyTotal = signMatchInfoDao.getExpendTotal(qySignId, 2,null);
//            if (null == expendQyTotal) {
//                expendQyTotal = 0d;
//            }
//            logger.info("企业部报销：" + expendQyTotal);
//            expendQyTotalDecimal = new BigDecimal(expendQyTotal);
            matchCount = new BigDecimal(signMatchInfoDao.getSignMatchCount(qySignId));
            logger.info("该公司已匹配" + matchCount.intValue() + "个证书");
//            expendQyTotalDecimalAvg = expendQyTotalDecimal
//                .divide(matchCount, 2, BigDecimal.ROUND_HALF_UP);
//            logger.info("每个匹配证书分摊企业部报销：" + expendQyTotalDecimalAvg.doubleValue());
            logger.info("企业部签约金额：" + signMatchInfo.getQiyePrice());
            logger.info("人才部签约金额：" + signMatchInfo.getRencaiPrice());
            pc.setqPrice(signMatchInfo.getQiyePrice());
            pc.setrPrice(signMatchInfo.getRencaiPrice());
//            allExpend = expendRcTotal + expendQyTotalDecimalAvg.doubleValue();
//            maxProfit =
//                signMatchInfo.getQiyePrice() - signMatchInfo.getRencaiPrice() - allExpend;
//            logger.info("该笔订单利润共：" + maxProfit.doubleValue());
//            if(maxProfit < 0d){
//                maxProfit = 0d;
//            }

            //int count = 0;
            for(String month : monthList){
                expendRcTotal = signMatchInfoDao.getExpendTotal(personSignId, 1,month);
                if (null == expendRcTotal) {
                    expendRcTotal = 0d;
                }
                logger.info("人才部" + month + "月报销：" + expendRcTotal);
                pc.setrEcpend(expendRcTotal);
                expendQyTotal = signMatchInfoDao.getExpendTotal(qySignId, 2,month);
                if (null == expendQyTotal) {
                    expendQyTotal = 0d;
                }
                logger.info("企业部" + month + "月报销：" + expendQyTotal);
                pc.setqExpend(expendQyTotal);

                expendQyMonTotalDecimal = new BigDecimal(expendQyTotal);

                expendQyMonTotalDecimalAvg = expendQyMonTotalDecimal
                    .divide(matchCount, 2, BigDecimal.ROUND_HALF_UP);

                logger.info("每个匹配证书分摊企业部报销：" + expendQyMonTotalDecimalAvg.doubleValue());

                allExpend = expendRcTotal + expendQyMonTotalDecimalAvg.doubleValue();

                logger.info(month + "月共报销：" + allExpend);

                maxProfit =
                    signMatchInfo.getQiyePrice() - signMatchInfo.getRencaiPrice() - allExpend;

                logger.info("该笔订单利润共：" + maxProfit.doubleValue());

                applyRcTotal = signMatchInfoDao.getRcApplySum(personSignId,month);
                if (null == applyRcTotal) {
                    applyRcTotal = 0d;
                }
                logger.info("人才部" + month + "转款一共：" + applyRcTotal);
                pc.setrApply(applyRcTotal);
                applyQyTotal = signMatchInfoDao.getQyApplySum(qySignId, personSignId, month);
                if (null == applyQyTotal) {
                    applyQyTotal = 0d;
                }
                logger.info("企业部" + month + "回款一共：" + applyQyTotal);
                pc.setqApply(applyQyTotal);
                if(applyQyTotal == 0){
                    continue;
                }
                if(applyQyTotal - applyRcTotal <= 0){
                    nowProfit = -allExpend;
                }else{
                    nowProfit =
                        applyQyTotal - applyRcTotal - allExpend;
                }

                Double checkedProfit = checkedMap.get(qySignId + "" + personSignId) == null ? 0d : checkedMap.get(qySignId + "" + personSignId);

                if(checkedProfit + nowProfit >= maxProfit){
                    thisProfit = maxProfit - checkedProfit;
                }else{
                    thisProfit = nowProfit;
                }
                Double countedProfit = signMatchInfoDao.getCountedProfit(matchId,month);
                if(null == countedProfit){
                    countedProfit = 0d;
                }
                logger.info("已校验业绩：" + countedProfit);
                pc.setCheckedProfit(countedProfit);
                if(countedProfit + thisProfit > maxProfit){
                    thisProfit = maxProfit - countedProfit;
                }
                Integer hisCount = signMatchInfoDao.getProfitHistory(matchId,month);
                if(hisCount == 0){
                    signMatchInfoDao.addProfitHistory(matchId,formatDouble2(thisProfit),month);
                } else {
                    signMatchInfoDao.updateProfitHistory(matchId,formatDouble2(thisProfit + countedProfit),month);
                }
//                if(month.equals("201901")){
//                    logger.info(thisProfit+""+qySignId+""+personSignId);
//                }
//                if(matchId == 44){
//                    logger.info(thisProfit+""+qySignId+""+personSignId);
//                }
//                if(signMatchInfo.getQiyeUserId() == 25){
//                    logger.info(thisProfit + "----------");
//                    logger.info("debug----------");
//                }
                checkedMap.put(qySignId + "" + personSignId,thisProfit + checkedProfit);
                logger.info("本次循环"+month+"业绩为：" + thisProfit);
                pc.setProfit(thisProfit);
                pcList.add(pc);
                qiyeMap = profitAccountMap.get(month);
                if (null == qiyeMap) {
                    qiyeMap = new HashMap<>();
                }
                String rcUserKey = signMatchInfo.getRencaiUserId() + "_1";
                String qyUserKey = signMatchInfo.getQiyeUserId() + "_2";

                nowRencaiProfit = qiyeMap.get(rcUserKey) == null ? 0d
                    : qiyeMap.get(rcUserKey);
                nowQyProfit = qiyeMap.get(qyUserKey) == null ? 0d
                    : qiyeMap.get(qyUserKey);
                BigDecimal rencaiDecimal = new BigDecimal(nowRencaiProfit).add(new BigDecimal(thisProfit)).setScale(2,BigDecimal.ROUND_HALF_UP);
                BigDecimal qiyeDecimal = new BigDecimal(nowQyProfit).add(new BigDecimal(thisProfit)).setScale(2,BigDecimal.ROUND_HALF_UP);
                qiyeMap.put(rcUserKey,rencaiDecimal.doubleValue());
                qiyeMap.put(qyUserKey,qiyeDecimal.doubleValue());
                profitAccountMap.put(month, qiyeMap);
                //count++;
            }

            logger.info("-----------------------------分割线-----------------------------------");
        }
        String updateTime = TimeUtils.getDateString(new Date(),TimeUtils.DATE_DAY_FORMAT);
        signMatchInfoDao.deleteProfit(updateTime);
        Profit pt = null;
        String[] userKey = null;
        for(Entry<String, Map<String, Double>> entry : profitAccountMap.entrySet()){
            for(Entry<String,Double> userEntry : entry.getValue().entrySet()){
                userKey = userEntry.getKey().split("_");
                pt = new Profit();
                pt.setAccount(entry.getKey());
                pt.setUserId(Integer.parseInt(userKey[0]));
                pt.setProfitType(Integer.parseInt(userKey[1]));
                pt.setProfit(userEntry.getValue());
                pt.setUpdateTime(updateTime);
                signMatchInfoDao.saveProfit(pt);
            }
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);

        String lastMonthLastDay = TimeUtils.getDateString(calendar.getTime(),TimeUtils.DATE_DAY_FORMAT);

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);

        String lastTwoMonthLastDay = TimeUtils.getDateString(calendar.getTime(),TimeUtils.DATE_DAY_FORMAT);

        logger.info(lastMonthLastDay);
        logger.info(lastTwoMonthLastDay);

        double all = 0.0;
        for(ProfitCheck tempPc : pcList){
            logger.info(tempPc.toString());
            all+=tempPc.getProfit();
        }
        logger.info(all + "");
        return profitAccountMap;
    }

    @RequestMapping("/getUserProfit.do")
    @ResponseBody
    public Map<String, Object> getUserProfit(HttpSession session) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Integer userId = (Integer) session.getAttribute("userId");
        String username = (String) session.getAttribute("username");
        User user = userDao.getUserById(username);
        List<String> accountList = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date nowMonth = c.getTime();
        c.add(Calendar.MONTH, -1);
        Date prevMonth = c.getTime();
        c.add(Calendar.MONTH, -1);
        Date prevTwoMonth = c.getTime();

        String account = TimeUtils.getDateString(nowMonth, TimeUtils.DATE_MONTH_FORMAT_SQL);
        String account1 = TimeUtils.getDateString(prevMonth, TimeUtils.DATE_MONTH_FORMAT_SQL);
        String account2 = TimeUtils.getDateString(prevTwoMonth, TimeUtils.DATE_MONTH_FORMAT_SQL);
        accountList.add(account);
        accountList.add(account1);
        accountList.add(account2);
        List<Profit> reList = null;
        String updateTime = TimeUtils.getDateString(new Date(),TimeUtils.DATE_DAY_FORMAT);
        if(user.getRoleId() == -1 || user.getRoleId() == 4){
            reList = signMatchInfoDao.getQyProfitTotal(updateTime);
        }else {
            reList = signMatchInfoDao.getUserProfit(userId, accountList, updateTime);
        }
        resultMap.put("data", reList);
        resultMap.put("count", reList.size());
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        return resultMap;
    }

    public List<String> getBeforeThreeMonth(){
        List<String> monthList = new ArrayList<>();
//        monthList.add("201809");
//        monthList.add("201810");
//        monthList.add("201811");
//        monthList.add("201812");
//        monthList.add("201901");
//        monthList.add("201903");
//        monthList.add("201904");
//        monthList.add("201902");
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.MONTH, -2);
        monthList.add(TimeUtils.getDateString(ca.getTime(),TimeUtils.DATE_MONTH_FORMAT_SQL));
        ca.add(Calendar.MONTH, 1);
        monthList.add(TimeUtils.getDateString(ca.getTime(),TimeUtils.DATE_MONTH_FORMAT_SQL));
        ca.add(Calendar.MONTH, 1);
        monthList.add(TimeUtils.getDateString(ca.getTime(),TimeUtils.DATE_MONTH_FORMAT_SQL));
        return monthList;
    }

    @RequestMapping("/getUserProfitList.do")
    @ResponseBody
    public Map<String, Object> getUserProfitList(@RequestParam("page") int page,
        @RequestParam("limit") int limit) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int startIndex = (page - 1) * limit;
        List<String> monthList = getBeforeThreeMonth();
        String updateTime = TimeUtils.getDateString(new Date(),TimeUtils.DATE_DAY_FORMAT);
        List<Profit> reList = signMatchInfoDao.getUserProfitList(startIndex,limit,monthList,updateTime);
        Integer count = signMatchInfoDao.getUserProfitListCount(monthList,updateTime);
        resultMap.put("data", reList);
        resultMap.put("count", count);
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        return resultMap;
    }

    @RequestMapping("/exportExcel.do")
    @ResponseBody
    public String exportDataExcel(String opTime){
        pcList = new ArrayList<>();
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date endDate = ca.getTime();
        Calendar ca1 = Calendar.getInstance();
        ca1.add(Calendar.MONTH, -2);
        ca1.set(Calendar.DAY_OF_MONTH, 1);
        Date begainDate = ca1.getTime();

        List<String> monthList = new ArrayList<>();
        monthList.add(opTime);
        List<SignMatchInfo> reList = signMatchInfoDao
            .getSignMatchList(TimeUtils.getDateString(begainDate, TimeUtils.DATE_DAY_FORMAT),
                TimeUtils.getDateString(endDate, TimeUtils.DATE_DAY_FORMAT));
        Double expendQyTotal = 0d;
        Double expendRcTotal = 0d;
        Double maxProfit = 0d;
        Double nowProfit = 0d;
        Double thisProfit = 0d;
        BigDecimal matchCount = null;
        BigDecimal expendQyMonTotalDecimal = null;
        BigDecimal expendQyMonTotalDecimalAvg = null;
        Integer matchId;
        Integer qySignId;
        Integer personSignId;
        Double applyRcTotal = 0d;
        Double applyQyTotal = 0d;
        Double nowRencaiProfit = 0d;
        Double nowQyProfit = 0d;
        Double allExpend = 0d;
        Map<String, Map<String, Double>> profitAccountMap = new HashMap<>();
        Map<Integer, Double> rencaiMap = null;
        Map<String, Double> qiyeMap = null;

        Map<String,Double> checkedMap = new HashMap<>();
        for (SignMatchInfo signMatchInfo : reList) {
            ProfitCheck pc = new ProfitCheck();
            matchId = signMatchInfo.getMatchId();
            qySignId = signMatchInfo.getSignId();
            personSignId = signMatchInfo.getPersonSignId();

            logger.info("企业部ID:" + qySignId);
            logger.info("人才部ID:" + personSignId);
            pc.setQid(qySignId);
            pc.setRid(personSignId);
            pc.setqName(signMatchInfo.getqName());
            pc.setrName(signMatchInfo.getrName());
            pc.setQuName(signMatchInfo.getQiyeUserId());
            pc.setRuName(signMatchInfo.getRencaiUserId());
            matchCount = new BigDecimal(signMatchInfoDao.getSignMatchCount(qySignId));
            logger.info("该公司已匹配" + matchCount.intValue() + "个证书");
            logger.info("企业部签约金额：" + signMatchInfo.getQiyePrice());
            logger.info("人才部签约金额：" + signMatchInfo.getRencaiPrice());
            pc.setqPrice(signMatchInfo.getQiyePrice());
            pc.setrPrice(signMatchInfo.getRencaiPrice());
            for(String month : monthList){
                expendRcTotal = signMatchInfoDao.getExpendTotal(personSignId, 1,month);
                if (null == expendRcTotal) {
                    expendRcTotal = 0d;
                }
                logger.info("人才部" + month + "月报销：" + expendRcTotal);
                pc.setrEcpend(expendRcTotal);
                expendQyTotal = signMatchInfoDao.getExpendTotal(qySignId, 2,month);
                if (null == expendQyTotal) {
                    expendQyTotal = 0d;
                }
                logger.info("企业部" + month + "月报销：" + expendQyTotal);
                pc.setqExpend(expendQyTotal);

                expendQyMonTotalDecimal = new BigDecimal(expendQyTotal);

                expendQyMonTotalDecimalAvg = expendQyMonTotalDecimal
                    .divide(matchCount, 2, BigDecimal.ROUND_HALF_UP);

                logger.info("每个匹配证书分摊企业部报销：" + expendQyMonTotalDecimalAvg.doubleValue());

                allExpend = expendRcTotal + expendQyMonTotalDecimalAvg.doubleValue();

                logger.info(month + "月共报销：" + allExpend);

                maxProfit =
                    signMatchInfo.getQiyePrice() - signMatchInfo.getRencaiPrice() - allExpend;

                logger.info("该笔订单利润共：" + maxProfit.doubleValue());

                applyRcTotal = signMatchInfoDao.getRcApplySum(personSignId,month);
                if (null == applyRcTotal) {
                    applyRcTotal = 0d;
                }
                logger.info("人才部" + month + "转款一共：" + applyRcTotal);
                pc.setrApply(applyRcTotal);
                applyQyTotal = signMatchInfoDao.getQyApplySum(qySignId, personSignId, month);
                if (null == applyQyTotal) {
                    applyQyTotal = 0d;
                }
                logger.info("企业部" + month + "回款一共：" + applyQyTotal);
                pc.setqApply(applyQyTotal);
                if(applyQyTotal == 0){
                    continue;
                }
                if(applyQyTotal - applyRcTotal <= 0){
                    nowProfit = -allExpend;
                }else{
                    nowProfit =
                        applyQyTotal - applyRcTotal - allExpend;
                }

                Double checkedProfit = checkedMap.get(qySignId + "" + personSignId) == null ? 0d : checkedMap.get(qySignId + "" + personSignId);

                if(checkedProfit + nowProfit >= maxProfit){
                    thisProfit = maxProfit - checkedProfit;
                }else{
                    thisProfit = nowProfit;
                }
                Double countedProfit = signMatchInfoDao.getCountedProfit(matchId,month);
                if(null == countedProfit){
                    countedProfit = 0d;
                }
                logger.info("已校验业绩：" + countedProfit);
                pc.setCheckedProfit(countedProfit);
                if(countedProfit + thisProfit > maxProfit){
                    thisProfit = maxProfit - countedProfit;
                }
                Integer hisCount = signMatchInfoDao.getProfitHistory(matchId,month);
                if(hisCount == 0){
                    signMatchInfoDao.addProfitHistory(matchId,formatDouble2(thisProfit),month);
                } else {
                    signMatchInfoDao.updateProfitHistory(matchId,formatDouble2(thisProfit + countedProfit),month);
                }
                checkedMap.put(qySignId + "" + personSignId,thisProfit + checkedProfit);
                logger.info("本次循环"+month+"业绩为：" + thisProfit);
                pc.setProfit(thisProfit);
                pcList.add(pc);
                qiyeMap = profitAccountMap.get(month);
                if (null == qiyeMap) {
                    qiyeMap = new HashMap<>();
                }
                String rcUserKey = signMatchInfo.getRencaiUserId() + "_1";
                String qyUserKey = signMatchInfo.getQiyeUserId() + "_2";

                nowRencaiProfit = qiyeMap.get(rcUserKey) == null ? 0d
                    : qiyeMap.get(rcUserKey);
                nowQyProfit = qiyeMap.get(qyUserKey) == null ? 0d
                    : qiyeMap.get(qyUserKey);
                BigDecimal rencaiDecimal = new BigDecimal(nowRencaiProfit).add(new BigDecimal(thisProfit)).setScale(2,BigDecimal.ROUND_HALF_UP);
                BigDecimal qiyeDecimal = new BigDecimal(nowQyProfit).add(new BigDecimal(thisProfit)).setScale(2,BigDecimal.ROUND_HALF_UP);
                qiyeMap.put(rcUserKey,rencaiDecimal.doubleValue());
                qiyeMap.put(qyUserKey,qiyeDecimal.doubleValue());
                profitAccountMap.put(month, qiyeMap);
                //count++;
            }

            logger.info("-----------------------------分割线-----------------------------------");
        }

        double all = 0.0;
        for(ProfitCheck tempPc : pcList){
            logger.info(tempPc.toString());
            all+=tempPc.getProfit();
        }
        logger.info(all + "");
        ProfitCheck pcAll = new ProfitCheck();
        pcAll.setProfit(all);
        pcList.add(pcAll);

        OutputStream out = null;
        String fileFullPath = ActivityConstant.DOWNLOAD_PATH + File.separator + "profit_" + System.currentTimeMillis() + ".xlsx";
        File tempExcelFile = new File(fileFullPath);
        try {
            if(!tempExcelFile.getParentFile().exists()){
                tempExcelFile.getParentFile().mkdirs();
            }
            out = new FileOutputStream(fileFullPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
        Sheet sheet = new Sheet(1,0,ProfitCheck.class);
        writer.write(pcList, sheet);
        writer.finish();
        return tempExcelFile.getName();
    }

    @GetMapping("/downloadOrderExcel.do")
    public void downloadOrderExcel(HttpServletResponse response, @RequestParam("fileName") String fileName){
        FileInputStream fis = null;
        OutputStream os = null;
        File file = new File(ActivityConstant.DOWNLOAD_PATH + File.separator + fileName);
        try {
            response.setHeader("content-disposition",
                "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            fis = new FileInputStream(file);
            os = response.getOutputStream();
            int count = 0;
            byte[] buffer = new byte[1024 * 1024];
            while ((count = fis.read(buffer)) != -1) {
                os.write(buffer, 0, count);
            }
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtil.close(os);
            IOUtil.close(fis);
            if(file.exists()){
                file.delete();
            }
        }
    }

    public static double formatDouble2(double d) {
        // 新方法，如果不需要四舍五入，可以使用RoundingMode.DOWN
        BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP);
        return bg.doubleValue();
    }

}
